package in.muton.edushare.validators;

import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.validator.AbstractValidator;

import com.edushare.common.dao.UsersDAO;
import com.edushare.common.orm.ORMInterface;

public class UserListValidator extends AbstractValidator<String> {
	
	private static final long serialVersionUID = -6108386083103548372L;
	ORMInterface ormInterface;
	
	public UserListValidator(ORMInterface ormInterface) {
		// TODO Auto-generated constructor stub
		this.ormInterface = ormInterface;
	}
	
	@Override
	protected void onValidate(IValidatable<String> validatable) {
		String inputString = validatable.getValue();
		if(inputString.trim().compareTo("") == 0){
			error(validatable);
		}
		UsersDAO dao = new UsersDAO(ormInterface);
		String[] userList = inputString.split(",");
		for(String s: userList){
			if(s.trim().compareTo("")==0){
				if(dao.getUserFromUserName(s) == null){
					error(validatable);
				}
			}
		}
	}
}
